﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;
using IFTRUE.EIMS.Helper;
using System.Collections;

namespace IFTRUE.EIMS.BLL
{
    /// <summary>
    /// 作用：企业信息操作类
    /// 作者：iftrue
    /// 日期：2013-08-26
    /// </summary>
   public class EnterpriseInfo
    {
      private MongoCollection collection;
      public EnterpriseInfo()
      {
          collection = MongoDbFactory.MongoDatabase.GetCollection("EnterpriseInfo");
      }

      /// <summary>
      /// 新增企业信息
      /// </summary>
      /// <param name="userInfoModel"></param>
      /// <returns></returns>
      public bool Add(Model.EnterpriseInfo enterpriseInfoModel)
      {
          var result = collection.Insert<Model.EnterpriseInfo>(enterpriseInfoModel);
          return result.Ok;
      }

      /// <summary>
      /// 更新企业信息
      /// </summary>
      /// <param name="enterpriseInfoModel">企业信息实体</param>
      /// <param name="strType">更新类型</param>
      /// <returns></returns>
      public bool Update(Model.EnterpriseInfo enterpriseInfoModel,string strType)
      {
          var query = MongoDB.Driver.Builders.Query.EQ("_id", enterpriseInfoModel.Id);
          //定义更新文档
          MongoDB.Driver.Builders.UpdateBuilder update = null;
          switch (strType)
          {
              case "qyjbqk":
                  update = MongoDB.Driver.Builders.Update.Set("Tbr", enterpriseInfoModel.Tbr).Set("Lxdh", enterpriseInfoModel.Lxdh).Set("Yx", enterpriseInfoModel.Yx)
                      .Set("Qymc", enterpriseInfoModel.Qymc).Set("Zczb", enterpriseInfoModel.Zczb).Set("Zzhjzqk", enterpriseInfoModel.Zzhjzqk).Set("Sfwgxjsqy", enterpriseInfoModel.Sfwgxjsqy).Set("Sfwlxsqy", enterpriseInfoModel.Sfwlxsqy);
                  break;
              case "qyryqktj":
                  update = MongoDB.Driver.Builders.Update.Set("Yyyg_zrs", enterpriseInfoModel.Yyyg_zrs).Set("Yyyg_dzys", enterpriseInfoModel.Yyyg_dzys).Set("Yyyg_ss", enterpriseInfoModel.Yyyg_ss)
                      .Set("Yyyg_bs", enterpriseInfoModel.Yyyg_bs).Set("Yyyg_lxry", enterpriseInfoModel.Yyyg_lxry).Set("Byxz_zrs", enterpriseInfoModel.Byxz_zrs)
                      .Set("Byxz_dzys", enterpriseInfoModel.Byxz_dzys).Set("Byxz_ss", enterpriseInfoModel.Byxz_ss).Set("Byxz_bs", enterpriseInfoModel.Byxz_bs).Set("Byxz_lxry", enterpriseInfoModel.Byxz_lxry);
                  break;
              case "zsqktj":
                  update = MongoDB.Driver.Builders.Update.Set("Ysqzsqk", enterpriseInfoModel.Ysqzsqk).Set("Xsqzsqk", enterpriseInfoModel.Xsqzsqk);
                  break;
              case "qyscxkzjcpzczqk":
                  update = MongoDB.Driver.Builders.Update.Set("Yhcpzczsl", enterpriseInfoModel.Yhcpzczsl).Set("Byxsqcpzczsl", enterpriseInfoModel.Byxsqcpzczsl)
                      .Set("Byxhdcpzczsl", enterpriseInfoModel.Byxhdcpzczsl).Set("Sfyhscxkz", enterpriseInfoModel.Sfyhscxkz)
                      .Set("Scxkzbgqk", enterpriseInfoModel.Scxkzbgqk).Set("Yhcpzczzsbh", enterpriseInfoModel.Yhcpzczzsbh);
                  break;
              case "qyzscqqktj":
                  update = MongoDB.Driver.Builders.Update.Set("Yyzscq_sbqk_fmzl", enterpriseInfoModel.Yyzscq_sbqk_fmzl).Set("Yyzscq_sbqk_syxxzl", enterpriseInfoModel.Yyzscq_sbqk_syxxzl)
                      .Set("Yyzscq_sbqk_wgzl", enterpriseInfoModel.Yyzscq_sbqk_wgzl).Set("Yyzscq_sbqk_rjzzq", enterpriseInfoModel.Yyzscq_sbqk_rjzzq)
                      .Set("Yyzscq_sbqk_pct", enterpriseInfoModel.Yyzscq_sbqk_pct).Set("Yyzscq_hpqk_fmzl", enterpriseInfoModel.Yyzscq_hpqk_fmzl)
                      .Set("Yyzscq_hpqk_syxxzl", enterpriseInfoModel.Yyzscq_hpqk_syxxzl).Set("Yyzscq_hpqk_wgzl", enterpriseInfoModel.Yyzscq_hpqk_wgzl)
                      .Set("Yyzscq_hpqk_rjzzq", enterpriseInfoModel.Yyzscq_hpqk_rjzzq).Set("Yyzscq_hpqk_pct", enterpriseInfoModel.Yyzscq_hpqk_pct)
                      .Set("Yyzscq_gmgwzlsl", enterpriseInfoModel.Yyzscq_gmgwzlsl).Set("Yyzscq_jshtjysl", enterpriseInfoModel.Yyzscq_jshtjysl)
                      .Set("Yyzscq_jshtjye", enterpriseInfoModel.Yyzscq_jshtjye).Set("Byxzzscq_sbqk_fmzl", enterpriseInfoModel.Byxzzscq_sbqk_fmzl)
                      .Set("Byxzzscq_sbqk_syxxzl", enterpriseInfoModel.Byxzzscq_sbqk_syxxzl).Set("Byxzzscq_sbqk_wgzl", enterpriseInfoModel.Byxzzscq_sbqk_wgzl)
                      .Set("Byxzzscq_sbqk_rjzzq", enterpriseInfoModel.Byxzzscq_sbqk_rjzzq).Set("Byxzzscq_sbqk_pct", enterpriseInfoModel.Byxzzscq_sbqk_pct)
                      .Set("Byxzzscq_hpqk_fmzl", enterpriseInfoModel.Byxzzscq_hpqk_fmzl).Set("Byxzzscq_hpqk_syxxzl", enterpriseInfoModel.Byxzzscq_hpqk_syxxzl)
                      .Set("Byxzzscq_hpqk_wgzl", enterpriseInfoModel.Byxzzscq_hpqk_wgzl).Set("Byxzzscq_hpqk_rjzzq", enterpriseInfoModel.Byxzzscq_hpqk_rjzzq)
                      .Set("Byxzzscq_hpqk_pct", enterpriseInfoModel.Byxzzscq_hpqk_pct).Set("Byxzzscq_gmgwzlsl", enterpriseInfoModel.Byxzzscq_gmgwzlsl)
                      .Set("Byxzzscq_jshtjysl", enterpriseInfoModel.Byxzzscq_jshtjysl).Set("Byxzzscq_jshtjye", enterpriseInfoModel.Byxzzscq_jshtjye);
                  break;
              case "qyqjyskjxmqk":
                  update = MongoDB.Driver.Builders.Update.Set("Ywcxmqk_qj_sl", enterpriseInfoModel.Ywcxmqk_qj_sl).Set("Ywcxmqk_qj_zzje", enterpriseInfoModel.Ywcxmqk_qj_zzje)
                      .Set("Zyxmqk_qj_sl", enterpriseInfoModel.Zyxmqk_qj_sl).Set("Zyxmqk_qj_zzje", enterpriseInfoModel.Zyxmqk_qj_zzje)
                      .Set("Byxzsqxmqk_qj_sl", enterpriseInfoModel.Byxzsqxmqk_qj_sl).Set("Byxzsqxmqk_qj_zzje", enterpriseInfoModel.Byxzsqxmqk_qj_zzje)
                      .Set("Byxzhpxmqk_qj_sl", enterpriseInfoModel.Byxzhpxmqk_qj_sl).Set("Byxzhpxmqk_qj_zzje", enterpriseInfoModel.Byxzhpxmqk_qj_zzje)
                      .Set("Ywcxmqk_shij_sl", enterpriseInfoModel.Ywcxmqk_shij_sl).Set("Ywcxmqk_shij_zzje", enterpriseInfoModel.Ywcxmqk_shij_zzje)
                      .Set("Zyxmqk_shij_sl", enterpriseInfoModel.Zyxmqk_shij_sl).Set("Zyxmqk_shij_zzje", enterpriseInfoModel.Zyxmqk_shij_zzje)
                      .Set("Byxzsqxmqk_shij_sl", enterpriseInfoModel.Byxzsqxmqk_shij_sl).Set("Byxzsqxmqk_shij_zzje", enterpriseInfoModel.Byxzsqxmqk_shij_zzje)
                      .Set("Byxzhpxmqk_shij_sl", enterpriseInfoModel.Byxzhpxmqk_shij_sl).Set("Byxzhpxmqk_shij_zzje", enterpriseInfoModel.Byxzhpxmqk_shij_zzje)
                      .Set("Ywcxmqk_shengj_sl", enterpriseInfoModel.Ywcxmqk_shengj_sl).Set("Ywcxmqk_shengj_zzje", enterpriseInfoModel.Ywcxmqk_shengj_zzje)
                      .Set("Zyxmqk_shengj_sl", enterpriseInfoModel.Zyxmqk_shengj_sl).Set("Zyxmqk_shengj_zzje", enterpriseInfoModel.Zyxmqk_shengj_zzje)
                      .Set("Byxzsqxmqk_shengj_sl", enterpriseInfoModel.Byxzsqxmqk_shengj_sl).Set("Byxzsqxmqk_shengj_zzje", enterpriseInfoModel.Byxzsqxmqk_shengj_zzje)
                      .Set("Byxzhpxmqk_shengj_sl", enterpriseInfoModel.Byxzhpxmqk_shengj_sl).Set("Byxzhpxmqk_shengj_zzje", enterpriseInfoModel.Byxzhpxmqk_shengj_zzje)
                      .Set("Ywcxmqk_gjj_sl", enterpriseInfoModel.Ywcxmqk_gjj_sl).Set("Ywcxmqk_gjj_zzje", enterpriseInfoModel.Ywcxmqk_gjj_zzje)
                      .Set("Zyxmqk_gjj_sl", enterpriseInfoModel.Zyxmqk_gjj_sl).Set("Zyxmqk_gjj_zzje", enterpriseInfoModel.Zyxmqk_gjj_zzje)
                      .Set("Byxzsqxmqk_gjj_sl", enterpriseInfoModel.Byxzsqxmqk_gjj_sl).Set("Byxzsqxmqk_gjj_zzje", enterpriseInfoModel.Byxzsqxmqk_gjj_zzje)
                      .Set("Byxzhpxmqk_gjj_sl", enterpriseInfoModel.Byxzhpxmqk_gjj_sl).Set("Byxzhpxmqk_gjj_zzje", enterpriseInfoModel.Byxzhpxmqk_gjj_zzje)
                      .Set("Shxxjtlbjmc", enterpriseInfoModel.Shxxjtlbjmc);
                  break;
              case "qyjdtj":
                  update = MongoDB.Driver.Builders.Update.Set("Zxjdqkhb", enterpriseInfoModel.Zxjdqkhb);
                  break;
              case "qyjjgk":
                  update = MongoDB.Driver.Builders.Update.Set("Qyndzsr", enterpriseInfoModel.Qyndzsr).Set("Qybyxzsr", enterpriseInfoModel.Qybyxzsr)
                      .Set("Qyndjlr", enterpriseInfoModel.Qyndjlr).Set("Qybyxzjlr", enterpriseInfoModel.Qybyxzjlr)
                      .Set("Qyndrdtr", enterpriseInfoModel.Qyndrdtr).Set("Qybyzxrdtr", enterpriseInfoModel.Qybyzxrdtr)
                      .Set("Qyckch", enterpriseInfoModel.Qyckch).Set("Qysjsjsf", enterpriseInfoModel.Qysjsjsf);
                  break;
              case "qyxstj":
                  update = MongoDB.Driver.Builders.Update.Set("Bndmqywcxs", enterpriseInfoModel.Bndmqywcxs).Set("Bndyjsxxs", enterpriseInfoModel.Bndyjsxxs).Set("Qyzjwyczjxsyy", enterpriseInfoModel.Qyzjwyczjxsyy);
                  break;
              case "qyxqjjy":
                  update = MongoDB.Driver.Builders.Update.Set("Zpxq", enterpriseInfoModel.Zpxq).Set("Pxxq", enterpriseInfoModel.Pxxq).Set("Qtxqjjy", enterpriseInfoModel.Qtxqjjy);
                  break;
          }
          update = update.Set("UpdateDate", DateTime.Now);
          var result = collection.Update(query, update);
          return result.Ok;
      }

      /// <summary>
      /// 更新企业信息(单个字段)
      /// </summary>
      /// <param name="enterpriseInfoModel">企业信息实体</param>
      /// <param name="fieldName">要更新的字段名称</param>
      /// <param name="type">要更新的字段类型</param>
      /// <returns></returns>
      public bool Update(Model.EnterpriseInfo enterpriseInfoModel, string fieldName, Type type)
      {
          var query = MongoDB.Driver.Builders.Query.EQ("Id", enterpriseInfoModel.Id);
          //定义更新文档
          string str = ObjectHelper.GeValueByName<Model.EnterpriseInfo>(enterpriseInfoModel, fieldName);

          IMongoUpdate update = null;
          if (type.Name == "DateTime")
          {
              update = MongoDB.Driver.Builders.Update.Set(fieldName, Convert.ToDateTime(str));
          }
          else
          {
              update = MongoDB.Driver.Builders.Update.Set(fieldName, str);
          }
          var result = collection.Update(query, update);
          return result.Ok;
      }

       /// <summary>
       /// 获取企业信息
       /// </summary>
      /// <param name="uid">用户ＩＤ</param>
       /// <returns></returns>
      public Model.EnterpriseInfo Get(string uid)
      {
          var query = MongoDB.Driver.Builders.Query.EQ("UserId", new MongoDB.Bson.ObjectId(uid));
          var result = collection.FindOneAs<Model.EnterpriseInfo>(query);

          return result;
      }

      /// <summary>
      /// 获取分页数据
      /// </summary>
      /// <param name="currIndex">当前页索引</param>
      /// <param name="pageSize">页行数</param>
      /// <param name="current">查询条件</param>
      /// <returns></returns>
      public string GetDataByPage(int currIndex, int pageSize, Hashtable ht)
      {
          MongoDB.Driver.IMongoQuery []querys = null;
          if (ht != null && ht.Count != 0)
          {
              querys = new MongoDB.Driver.QueryDocument[ht.Count + 1];
              int i = 0;
              foreach (DictionaryEntry de in ht)
              {
                  //如果按用户名称查询，则需要先查询用户表
                  if (de.Key.ToString() == "Qymc")
                  {
                      querys[i] = MongoDB.Driver.Builders.Query.Matches("Qymc", new MongoDB.Bson.BsonRegularExpression("/" + de.Value + "/"));
                  }
                  i++;
              }
              querys[ht.Count] = MongoDB.Driver.Builders.Query.NE("Qymc", MongoDB.Bson.BsonNull.Value);
          }
          else
          {
              querys = new MongoDB.Driver.QueryDocument[1];
              querys[0] = MongoDB.Driver.Builders.Query.NE("Qymc",MongoDB.Bson.BsonNull.Value);
          }
          long recordCount = 0;
          //排除还没有填写的企业信息
          var result = new Common().GetMongoCursorByPage<Model.EnterpriseInfo>(collection, MongoDB.Driver.Builders.Query.And(querys), MongoDB.Driver.Builders.SortBy.Descending("Qymc"), currIndex, pageSize, ref recordCount);
          StringBuilder s = new StringBuilder();
          if (result != null)
          {
              foreach (var v in result)
              {
                  s.Append(string.Format("<tr target='pid' rel='" + v.UserId + "'><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr>",
                      v.Qymc, v.Zczb.ToString(), v.Yyyg_zrs.ToString(), v.Yhcpzczsl.ToString(), v.Qyndzsr.ToString(),v.UpdateDate.ToString("yyyy-MM-dd HH:mm:ss")));
              }
          }
          return recordCount.ToString() + "|" + s.ToString();
      }
    }
}
